package com.hpin.fansea.ui;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.fragment.CircleFragment;
import com.hpin.fansea.fragment.HomePageFragment;
import com.hpin.fansea.fragment.MyFragement;
import com.hpin.fansea.fragment.ServiceFragment;
import com.hpin.fansea.utils.Constant;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Aaron on 15/9/7.
 */
public class HomeActivity extends BaseActivity {

//    private FrameLayout fl_base;
    /**
     * fragment 管理者
     */
    private FragmentManager manager;
    private HomePageFragment homePageFragment;
    private ServiceFragment serviceFragment;
    private CircleFragment circleFragment;
    private MyFragement myFragement;
    private TextView tv_message;
    private TextView tv_engagedService;
    private TextView tv_shoppingCircle;
    private TextView tv_my;
    private ImageView iv_home;
    private ImageView iv_engagedService;
    private ImageView iv_shoppingCircle;
    private ImageView iv_my;

//    private Intent mIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);

        mApplication.addActivity(this);

        initWidget();

        initFragment();

////        友盟检查更新
//        UmengUpdateAgent.setUpdateOnlyWifi(false);
//        UmengUpdateAgent.update(this);
//        UpdateConfig.setDebug(true);//debug模式,发布时删除

    }

    private void initWidget() {

//        fl_base = (FrameLayout) findViewById(R.id.fl_base);

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.app_name_sgj));

        RelativeLayout rl_home = (RelativeLayout) findViewById(R.id.rl_home);
        RelativeLayout rl_engagedService = (RelativeLayout) findViewById(R.id.rl_engagedService);
        RelativeLayout rl_shoppingCircle = (RelativeLayout) findViewById(R.id.rl_shoppingCircle);
        RelativeLayout rl_my = (RelativeLayout) findViewById(R.id.rl_my);

        iv_home = (ImageView) findViewById(R.id.iv_home);
        iv_engagedService = (ImageView) findViewById(R.id.iv_engagedService);
        iv_shoppingCircle = (ImageView) findViewById(R.id.iv_shoppingCircle);
        iv_my = (ImageView) findViewById(R.id.iv_my);


        tv_message = (TextView) findViewById(R.id.tv_message);
        tv_engagedService = (TextView) findViewById(R.id.tv_engagedService);
        tv_shoppingCircle = (TextView) findViewById(R.id.tv_shoppingCircle);
        tv_my = (TextView) findViewById(R.id.tv_my);

        rl_home.setOnClickListener(this);
        rl_engagedService.setOnClickListener(this);
        rl_shoppingCircle.setOnClickListener(this);
        rl_my.setOnClickListener(this);

    }

    private void initFragment() {

        manager = getFragmentManager();

        iv_home.setSelected(true);
        tv_message.setTextColor(getResources().getColor(R.color.title_color));

        chooseFragment(0);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.rl_home://首页

                //把所有的图标都变灰
                setSelectFalse();

                chooseFragment(0);

                //TODO 改变字体颜色和图片
                iv_home.setSelected(true);
                tv_message.setTextColor(getResources().getColor(R.color.title_color));
                break;

            case R.id.rl_engagedService://特约服务

                //把所有的图标都变灰
                setSelectFalse();

                chooseFragment(1);

                iv_engagedService.setSelected(true);
                tv_engagedService.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case R.id.rl_shoppingCircle://商圈

                //把所有的图标都变灰
                setSelectFalse();

                chooseFragment(2);

                iv_shoppingCircle.setSelected(true);
                tv_shoppingCircle.setTextColor(getResources().getColor(R.color.title_color));
                break;
            case R.id.rl_my: //我的

                //把所有的图标都变灰
                setSelectFalse();

                chooseFragment(3);

                iv_my.setSelected(true);
                tv_my.setTextColor(getResources().getColor(R.color.title_color));

                break;


        }
    }

    /**
     * 选择对应的fragment
     *
     * @param index 对应的fragment的角标
     */
    private void chooseFragment(int index) {

        FragmentTransaction transaction = null;
        if (manager != null) {
            transaction = manager.beginTransaction();
        }
        // 先移除掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
//        removeFragments(transaction);

        if (transaction == null)
            return;

        switch (index) {

            case 0:
                //消息
                if (homePageFragment == null)
                    homePageFragment = new HomePageFragment();

                transaction.replace(R.id.fl_base, homePageFragment, Constant.HOME);

                break;

            case 1:

                if (serviceFragment == null)
                    serviceFragment = new ServiceFragment();

                transaction.replace(R.id.fl_base, serviceFragment, Constant.ENGAGEDSERVICE);

                break;

            case 2:
                if (circleFragment == null)
                    circleFragment = new CircleFragment();

                transaction.replace(R.id.fl_base, circleFragment, Constant.SHOPPINGCIRCLE);

                break;
            case 3:

                if (myFragement == null)
                    myFragement = new MyFragement();

                transaction.replace(R.id.fl_base, myFragement, Constant.MY);

                break;

        }

        if (transaction != null) {
//            transaction.commit();
            transaction.commitAllowingStateLoss();
        }

    }

    /**
     * 移除fragment
     *
     * @param transaction
     */
    private void removeFragments(FragmentTransaction transaction) {
        if (transaction == null)
            return;

        if (homePageFragment != null) {
            transaction.remove(homePageFragment);
        }
        if (serviceFragment != null) {
            transaction.remove(serviceFragment);
        }
        if (circleFragment != null) {
            transaction.remove(circleFragment);
        }
        if (myFragement != null) {
            transaction.remove(myFragement);
        }

    }


    /**
     * 把所有的图标都变灰
     * 字体还原
     */
    private void setSelectFalse() {

        iv_home.setSelected(false);
        iv_engagedService.setSelected(false);
        iv_shoppingCircle.setSelected(false);
        iv_my.setSelected(false);

        tv_message.setTextColor(getResources().getColor(R.color.text_color));
        tv_engagedService.setTextColor(getResources().getColor(R.color.text_color));
        tv_shoppingCircle.setTextColor(getResources().getColor(R.color.text_color));
        tv_my.setTextColor(getResources().getColor(R.color.text_color));

    }

    private long mPressedTime = 0;

    @Override
    public void onBackPressed() {

        long mNowTime = System.currentTimeMillis();//获取第一次按键时间

        if ((mNowTime - mPressedTime) > 2000) {//比较两次按键时间差

            showToast("再按一次退出程序");

            mPressedTime = mNowTime;

        } else {//退出程序

            finish();
            mApplication.exit();
        }
    }


    @Override
    public void onDestroy() {

        super.onDestroy();
    }

    public void onResume() {
        super.onResume();


        MobclickAgent.onResume(this);       //统计时长
    }

    public void onPause() {
        super.onPause();

        MobclickAgent.onPause(this);

    }
}
